<template>
  <div class="product">
    <header class="page-header">
      产品管理
    </header>

    <div class="btn-group">
      <el-button type="primary" @click="newPorduct">新增产品</el-button>
    </div>

    <el-form :inline="true"   class="userSearch">
      <el-form-item>
      </el-form-item>
      <el-form-item label="搜索">
        <el-input v-model="productSearch.name" placeholder="商品名/缩写/编号"></el-input>
      </el-form-item>
      <el-form-item label="业务类别">
        <template>
          <el-select v-model="productSearch.contentid" @change="query_product" placeholder="请选择">
            <el-option
              key="0"
              label="全部"
              :value="0">
            </el-option>
            <el-option
              v-for="item in content_list"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="query_product()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="pro_list" border style="width: 100%">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="name" label="预览图">
        <template slot-scope="scope">
          <img :src="scope.row.img[0].url" alt="预览图" style="width:50px;">
        </template>
      </el-table-column>
      <el-table-column prop="name" label="商品名"> </el-table-column>
      <el-table-column prop="shortname" label="缩写"> </el-table-column>
      <el-table-column prop="remark" label="描述"> </el-table-column>
      <el-table-column prop="contentid" label="类别"> </el-table-column>
      <el-table-column prop="price" label="参考价"> </el-table-column>
      <el-table-column label="是否推荐">
        <template slot-scope="scope">
          <span>{{scope.row.isrecommend ? '推荐' : '未推荐'}}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="modifyProduct(scope.row.id)">修改产品</el-button>
          <el-button type="text" size="small" @click="setDele(scope.row.id, scope.row.isdel)">删除产品</el-button>
          <el-button type="text" size="small" @click="setRecommend(scope.row.id, scope.row.isrecommend)">{{scope.row.isrecommend ? '取消推荐' : '设为推荐'}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="page-pagination"
      @current-change="current_change"
      background
      layout="prev, pager, next"
      :page-size="7"
      :total="parseInt(total)">
    </el-pagination>
    <el-dialog
      :title="this.newPorductParams.pid ? '修改产品' : '新增产品'"
      :visible.sync="newVisible"
      :append-to-body="true"
      :before-close="handleClose">
      <el-upload
        action="https://www.qiniu.com/"
        list-type="picture-card"
        :file-list="this.newPorductParams.img"
        :on-remove="handleRemove"
        :auto-upload="false"
        :multiple="true"
        :on-change="fileChange">
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-form :inline="true" class="product-edit">
        <el-form-item label="名称">
          <el-input v-model="newPorductParams.name" placeholder="输入名称"></el-input>
        </el-form-item>
        <el-form-item label="缩写">
          <el-input v-model="newPorductParams.shortname" placeholder="输入缩写"></el-input>
        </el-form-item>
        <el-form-item label="参考价">
          <el-input v-model="newPorductParams.price" placeholder="输入参考价"></el-input>
        </el-form-item>
        <el-form-item label="业务类别">
          <template>
            <el-select v-model="newPorductParams.contentid" placeholder="请选择">
              <el-option
                v-for="item in content_list"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="描述">
          <el-input type="textarea" v-model="newPorductParams.remark" placeholder="输入产品描述"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="newVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmUpload">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import qiniu from 'qiniu-js'
export default {
  name: "user",
  data() {
    return {
      file: [],
      sfile:[],
      newVisible: false,
      productSearch: {
        name: "",
        contentid: 0,
        current_page: 1,
        pagesize: 7
      },
      newPorductParams:{
        name: '',
        shortname: '',
        img: [],
        price: '',
        remark: '',
        contentid: ''
      }
    };
  },
  computed: {
    ...mapGetters({
      pro_list: "product/pro_list",
      total: "product/total",
      content_list: "product/content_list"
    })
  },
  methods: {
    productSearchHandle(id) {
      console.log(id)
    },
    handleClose() {
      this.newVisible = false
    },
    handleRemove(file) {
      this.newPorductParams.img = this.newPorductParams.img.filter(img => {
        return img.url !== file.url
      })
    },
    resetUserInfo() {
      this.userInfo = {
        phone: "",
        userId: ""
      }
    },
    current_change(current) {
      this.productSearch.current_page = current
      this.query_product()
    },
    query_product() {
      this.$store.dispatch('product/query_product', this.productSearch)
    },
    setRecommend(id, isrecommend) {
      isrecommend = Math.abs(parseInt(isrecommend) - 1)
      this.$store.dispatch('product/set_recommend', {id: id, isrecommend: isrecommend, other: this.productSearch})
    },
    setDele(id, isdel) {
      isdel = Math.abs(parseInt(isdel) - 1)
      this.$store.dispatch('product/set_dele', {id: id, isdel: isdel, other: this.productSearch})
    },
    fileChange(file, fileList){
      fileList = fileList || []
      this.sfile = fileList.map(function (file) {
        return file.raw
      })
    },
    confirmUpload() {
      var form = new FormData();
      this.sfile = this.sfile.filter(function (file) {
        return file
      })
      this.sfile.forEach(function (file, index){
        form.append('file' + index, file)
      })
      for (var i in this.newPorductParams) {
        form.append(i, this.newPorductParams[i])
      }
      if(!this.newPorductParams.id) {
        this.$store.dispatch('product/upload', {form: form})
      }else{
        this.$store.dispatch('product/modify_product', {form: form})
      }

    },
    newPorduct() {
      this.newPorductParams = {
        name: '',
        shortname: '',
        img: [],
        price: '',
        remark: '',
        contentid: ''
      }
      this.newVisible = true
    },
    modifyProduct(id) {
      this.newPorductParams = {
        id: id,
        name: '',
        shortname: '',
        img: '',
        price: '',
        remark: '',
        contentid: ''
      },
      this.$store.dispatch('product/product_detail')
    },
    getContent() {
      this.$store.dispatch('product/query_content')
    }
  },
  created() {
    this.query_product()
    this.getContent()
    this.$store.commit('product/SET_VM', {vm: this})
  }
};
</script>

<style lang="scss">
.product {
  .btn-group {
    text-align: center;
    margin-top: 20px;
    margin-bottom: 20px;
  }
  .userSearch {
    text-align: center;
  }

}
.product-edit{
  label{
    width: 100px!important;
  }
  input.el-input__inner{
    width: 200px!important;
  }
}
</style>
